const mongoose = require('mongoose');

const orderSchema = new mongoose.Schema({
  buyer: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  seller: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  product: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Product',
    required: true
  },
  status: {
    type: String,
    enum: ['待见面交易', '已完成', '已取消', '已强制取消'],
    default: '待见面交易'
  },
  price: {
    type: Number,
    required: true
  },
  meetingInfo: {
    location: String,
    time: Date
  },
  contactInfo: {
    phone: String,
    wechat: String
  },
  review: {
    rating: {
      type: Number,
      min: 1,
      max: 5
    },
    comment: String,
    createdAt: Date
  },
  cancelReason: String,
  cancelledBy: {
    type: String,
    enum: ['buyer', 'seller', 'admin']
  },
  cancelledAt: Date,
  adminRemark: {
    type: String,
    default: ''
  },
  adminAction: {
    action: {
      type: String,
      enum: ['强制取消', '恢复订单', '其他']
    },
    reason: String,
    actionTime: Date,
    adminId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    }
  },
  disputeInfo: {
    isDisputed: {
      type: Boolean,
      default: false
    },
    reason: String,
    reportedBy: {
      type: String,
      enum: ['buyer', 'seller', 'system']
    },
    reportedAt: Date,
    resolution: String,
    resolvedAt: Date,
    resolvedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    }
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

const Order = mongoose.model('Order', orderSchema);
module.exports = Order; 